#!/bin/sh
#########################################################################
# START
# File Name: entrypoint.sh
# Author: Jetsung Chan
# Email:  skiy@jetsung.com
# Created: 2019/09/02
# Updated: 2022/03/08
#########################################################################

install_tools() {
    sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list
    sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
    apt-get update -y
    apt-get install -y gcc \
        g++ \
        autoconf \
        automake \
        make \
        cmake
}

clear_tools() {
    apt-get remove -y gcc \
        g++ \
        autoconf \
        automake \
        make \
        cmake
    apt-get autoremove -y
    apt-get autoclean -y
    apt-get clean -y
}

# Add PHP Extension
install_extensions() {
    if [ -f "/app/extension.sh" ] && [ ! -f /app/.installed ]; then
        pushd /app > /dev/null
            install_tools

            bash extension.sh
            echo $(date "+%F %T") >> /app/.installed

            #clear_tools
        popd > /dev/null
    fi
}

# 启动redis
service redis-server start

if [ "${1}" = "-D" ]; then
    install_extensions 2>&1 | tee ./install.log 

    # start supervisord and services
    exec /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
else
    exec "$@"
fi
